#include <plib.h>
#include "plot.h"
#include "logdata.h"
#include "matchingdata.h"
#include "opendxdata.h"
#include "geomview.h"

#define MAX_M 1000
#define MAX_S 256
#define BEFORE_ET 1
#define AFTER_ET 2

char ext[10][5];

int scan_choice(int *c);
int getIds(int* methods,int* ids, int nb, logF *log,int best,int worst);
int getMethods(int *methods,logF *log);
int getMatchings(int *methods,logF *log,char* best, char* worst);

int timingfile(logFinfo* info);
int logfile(logFinfo* info,int et);
int matchstats(logFinfo* info);
int refinestats(logFinfo* info);

int dxoutput(logFinfo* info);
int refinedxoutput(logFinfo* info);
int geomviewapp(logFinfo* info);
int geomviewpart(logFinfo* info);

int main(int argc, char *argv[]){ 
  int log =-1;  
  logFinfo* info;

  WARNING((argc < 2,"USAGE : pagridplot file [coordinates_file]\n\nex : pagridplot grid20x20.graph.net_cost.proc_ratio [grid20x20.coords]\nthis will plot data from :\n - grid20x20.graph.net_cost.proc_ratio.log\n - grid20x20.graph.net_cost.proc_ratio.time.log\n - grid20x20.graph.net_cost.proc_ratio.bet.log\nif the coordinate file is provided you would be able to generate opendx file to visualize matchings\n"),1);

  /* plot files extensions */
  strcpy(ext[PNG], "png");
  strcpy(ext[PS], "ps");
  strcpy(ext[EPS], "eps");
  strcpy(ext[PDF], "pdf");

  WARNING(((info = LD_readInfo(argc,argv)) == NULL,"Error in LD_readInfo\n"),1);

  do{
    switch(log){
    case 1:
      timingfile(info);
      break;
    case 2:
#ifdef DEBUG
      dbg_freemem();
#endif
      logfile(info,BEFORE_ET);
#ifdef DEBUG
      dbg_isemptymem();
#endif
      break;
    case 3:
#ifdef DEBUG
      dbg_freemem();
#endif
      logfile(info,AFTER_ET);
#ifdef DEBUG
      dbg_isemptymem();
#endif
      break;
    case 4:
#ifdef DEBUG
      dbg_freemem();
#endif
      matchstats(info);
#ifdef DEBUG
      dbg_isemptymem();
#endif
      break;
    case 5:
#ifdef DEBUG
      dbg_freemem();
#endif
      if(argc == 3)
	dxoutput(info);
#ifdef DEBUG
      dbg_isemptymem();
#endif
      break;
    case 6:
#ifdef DEBUG
      dbg_freemem();
#endif
      refinestats(info);
#ifdef DEBUG
      dbg_isemptymem();
#endif
      break;
    case 7:
#ifdef DEBUG
      dbg_freemem();
#endif
      if(argc == 3)
	refinedxoutput(info);
#ifdef DEBUG
      dbg_isemptymem();
#endif
      break;
    case 8:
      if(argc == 3)
	geomviewapp(info);
      break;
    case 9:
      if(argc == 3)
	geomviewpart(info);
      break;
    }
    printf("-------------------------------------------------\n");
    printf("| %45s |\n","Choose log file");
    printf("-------------------------------------------------\n");
    printf("| %2d | %40s |\n",1,"Timing");
    printf("| %2d | %40s |\n",2,"Before execution time load balancing");
    printf("| %2d | %40s |\n",3,"After execution time load balancing");
    printf("-------------------------------------------------\n");
    printf("| %45s |\n","Matching information");
    printf("-------------------------------------------------\n");
    printf("| %2d | %40s |\n",4,"Stats");
    if(argc == 3)
      printf("| %2d | %40s |\n",5,"Generate OpenDx files for matchings");
    printf("-------------------------------------------------\n");
    printf("| %45s |\n","Refinement information");
    printf("-------------------------------------------------\n");
    printf("| %2d | %40s |\n",6,"Stats");
    if(argc == 3)
      printf("| %2d | %40s |\n",7,"Generate OpenDx files for refinement");
    if(argc == 3){
      printf("-------------------------------------------------\n");
      printf("| %45s |\n","Geomview");
      printf("-------------------------------------------------\n");
      printf("| %2d | %40s |\n",8,"Application graph");
      printf("| %2d | %40s |\n",9,"Final partition");
    }
    printf("-------------------------------------------------\n");
    printf("| %2d | %40s |\n",10,"Quit");
    printf("-------------------------------------------------\n");
    printf("choice : ");
  }while(scan_choice(&log) && log != 10);
  printf("\n");

#ifndef DEBUG
  LD_freeInfo(info);
#endif
  return 0;
}

int getIds(int* methods,int* ids, int nb, logF *log,int best,int worst){
  int *m_id,*s_id;
  int i,j,k;
  /* look back to find which method and which sample is each id */
  m_id = U_malloc(nb,int,"getIds error code 1\n");
  s_id = U_malloc(nb,int,"getIds error code 2\n");

  for(i=0;i<nb;i++){
    for(j=0;j<log->m;j++){
      for(k=0;k<log->s[j];k++){
	if(ids[i] == log->id[j][k]){
	  m_id[i] = j;
	  s_id[i] = log->matching[j][k];
	}
      }
    }
  }
  printf("--------------------------------------------------------------------------------------------\n");
  printf("| %6s | %30s | %5s | %30s | %5s |\n","","method","id","matching","");
  printf("--------------------------------------------------------------------------------------------\n");
  printf("| %6d | %71s |\n",-1,"All");
  for(i=0;i<nb;i++){
    if(s_id[i] != -1 && log->coarse[s_id[i]]){
      printf("| %6d | %30s | %5d | %30s | %5s |\n",i,
	     log->methods[m_id[i]],ids[i],log->coarse[s_id[i]],
	     (best == ids[i]?"best":(worst == ids[i]?"worst":"")));
    }/*else{
      printf("| %6d | %30s | %5d | %30s | %5s |\n",i,
	     log->methods[m_id[i]],ids[i],"",
	     (best == ids[i]?"best":(worst == ids[i]?"worst":"")));
	     }*/
  }
  printf("--------------------------------------------------------------------------------------------\n");
  printf("enter method follow by enter, ctrl+D to end\nmethod : ");
  i = 0;
  while(scan_choice(&j)){
    if(j == -1){
      j = nb;
      for(k=0;k<nb;k++){
	methods[k]=k;
      }
      U_free("getIds error code 1\n",&m_id,&s_id,LEND);
      return j;
    }
    methods[i] = j;
    printf("[%d",methods[0]);
    for(k=1;k<=i;k++){
      printf(", %d",methods[k]);
    }
    printf("]\n");
    printf("method : ");
    i++;
  }
  printf("\n");
  U_free("getIds error code 1\n",&m_id,&s_id,LEND);
  return i;
}

int getMethods(int *methods,logF *log){
  int i,j,k;
  printf("-------------------------------------------\n");
  printf("| %6s | %30s |\n","method","name");
  printf("-------------------------------------------\n");
  printf("| %6d | %30s |\n",-1,"All");
  for(i=0;i<log->m;i++){
    printf("| %6d | %30s |\n",i,log->methods[i]);
  }
  printf("-------------------------------------------\n");
  printf("enter method follow by enter, ctrl+D to end\nmethod : ");
  i = 0;
  while(scan_choice(&j)){
    if(j == -1){
      j = log->m;
      for(k=0;k<log->m;k++){
	methods[k]=k;
      }
      return j;
    }
    methods[i] = j;
    printf("[%d",methods[0]);
    for(k=1;k<=i;k++)
      printf(", %d",methods[k]);
    printf("]\n");
    printf("method : ");
    i++;
  }
  printf("\n");
  return i;
}

int getMatchings(int *methods,logF *log,char *best, char *worst){
  int i,j,k,l;
  char scheme[MAX_S];
  printf("---------------------------------------------------------------------------------------------\n");
  printf("| %8s | %70s | %5s |\n","matching","filename","");
  printf("---------------------------------------------------------------------------------------------\n");
  printf("| %8d | %70s | %5s |\n",-1,"All","");
  for(i=0;i<log->c;i++){
    /* find scheme of matching */
    for(j=0;j<log->m;j++){
      for(l=0;l<log->s[j];l++){
	if(log->matching[j][l] == i){
	  strcpy(scheme,log->methods[j]);
	  /* remove refinement info of scheme everything after second - */
	  k = 4;
	  for(j=0;j<strlen(scheme);j++){
	    if(scheme[j] == '-'){
	      k--;
	      if(k==0){
		scheme[j] = '\0';
		break;
	      }
	    }	  
	  }
	  goto getmatchingbreak;
	}
      }
    }

  getmatchingbreak:
    printf("| %8d | %29s  %39s | %5s |\n",i,scheme,log->coarse[i],(log->coarse[i] == best ? "best" : (log->coarse[i] == worst ? "worst" : "")));
  }
  printf("---------------------------------------------------------------------------------------------\n");
  printf("enter method follow by enter, ctrl+D to end\nmethod : ");
  i = 0;
  while(scan_choice(&j)){
    if(j == -1){
      j = log->c;
      for(k=0;k<j;k++)
	methods[k] = k;
      return j;
    }
    methods[i] = j;
    printf("[%d",methods[0]);
    for(k=1;k<=i;k++){
      printf(", %d",methods[k]);
    }
    printf("]\n");
    printf("method : ");
    i++;
  }
  printf("\n");
  return i;
}

int timingfile(logFinfo* info){
  char filename[MAX_S];
  FILE *fp;
  plot *draw;
  logF *data = NULL;
  ctrl ctrl;
  int x, y, choice = -1, choice2 = -1;
  char label[5][MAX_S], stat_file[MAX_S], stdout_file[MAX_S], plot_file[MAX_S];
  char file[5][MAX_S];

  /* initialisation */
  strcpy(filename,info->base);
  strcat(filename,".time.log");

  WARNING(((fp = fopen(filename,"r")) == NULL,"Cannot open file %s\n",filename),1);

  /* labels */
  strcpy(label[TIME_COARSENING],"Coarsening Time");
  strcpy(label[TIME_UNCOARSENING],"Refinement Time");
  strcpy(label[TIME_EXECTIME],"Execution time load balancing");
  strcpy(file[TIME_COARSENING],".coa");
  strcpy(file[TIME_UNCOARSENING],".unco");
  strcpy(file[TIME_EXECTIME],".exec");

  /* filenames */
  strcpy(stdout_file,"stdout");
  strcpy(stat_file,info->base);
  strcat(stat_file,".time.stat.log");

  /* options */
  /* default */
  ctrl.stat_out  = FILES;
  ctrl.stat_file = stat_file;
  ctrl.plot_out  = STDOUT;
  ctrl.plot_file = plot_file;
  strcpy(plot_file,stdout_file);
  
  x = TIME_COARSENING;
  y = TIME_UNCOARSENING;

  ctrl.multiple_win = NO;

  /* menu to choose options */
  do{
    switch(choice){
      /****************************************
       * changing parameters
       ****************************************/
      /* Statistics output */
    case 1:
      if(data == NULL){
	do{
	  printf("-------------------------------------------------------------------------------\n");
	  printf("| %75s |\n","Statistics output");
	  printf("-------------------------------------------------------------------------------\n");
	  printf("| %2d | %70s |\n",1,stdout_file);
	  printf("| %2d | %70s |\n",2,stat_file);
	  printf("-------------------------------------------------------------------------------\n");
	  printf("choice : ");
	}while(!scan_choice(&choice2) && (choice2 != 1 || choice2 != 2));
	switch(choice2){
	case 1:
	  ctrl.stat_out = STDOUT;
	  ctrl.stat_file = stdout_file;
	  break;
	case 2:
	  ctrl.stat_out = FILES;
	  ctrl.stat_file = stat_file;
	}
	break;
      }
      break;
      /* Plots output */
    case 2:
      do{
	printf("-------------------\n");
	printf("| %15s |\n","Plots output");
	printf("-------------------\n");
	printf("| %2d | %10s |\n",STDOUT,stdout_file);
	printf("| %2d | %10s |\n",PNG,"PNG");
	printf("| %2d | %10s |\n",PS,"PS");
	printf("| %2d | %10s |\n",EPS,"EPS");
	printf("| %2d | %10s |\n",PDF,"PDF");
	printf("-------------------\n");
	printf("choice : ");
      }while(!scan_choice(&choice2) && (choice2 != STDOUT || choice2 != PNG || choice2 != PS || choice2 != EPS || choice2 != PDF));
      ctrl.plot_out = choice2;
      if(choice2 != STDOUT)
	sprintf(plot_file,"%s%s%s.%s",info->base,file[x],file[y],ext[choice2]);
      else
	strcpy(plot_file,stdout_file);
      break;
      /* X values */
    case 3:
    case 4:
      do{
	printf("-------------------------------------------------------------------------------\n");
	if(choice == 3){
	  printf("| %75s |\n","X values");
	}else{
	  printf("| %75s |\n","Y values");
	}
	printf("-------------------------------------------------------------------------------\n");
	printf("| %2d | %70s |\n",TIME_COARSENING,label[TIME_COARSENING]);
	printf("| %2d | %70s |\n",TIME_UNCOARSENING,label[TIME_UNCOARSENING]);
	printf("| %2d | %70s |\n",TIME_EXECTIME,label[TIME_EXECTIME]);
	printf("-------------------------------------------------------------------------------\n");
	printf("choice : ");
      }while(!scan_choice(&choice2) && (choice2 != TIME_EXECTIME || choice2 != TIME_UNCOARSENING || choice2 !=  TIME_COARSENING));
      if(choice == 3){
	x = choice2;
      }else{
	y = choice2;
      }
      if(ctrl.plot_out != STDOUT)
	sprintf(plot_file,"%s%s%s.%s",info->base,file[x],file[y],ext[ctrl.plot_out]);
      break;
      /****************************************
       * Running with parameters
       ****************************************/
    case 6:
      if(data == NULL)
	data = LD_readTime(fp,&ctrl);

      draw = LD_getPlotXY(data,x,y,NULL,0,BY_COARSENING,label[x],label[y],
			  "Timing",NO_BORDER,GRID,KEY,XTICS,YTICS);
      PLOT_draw(draw,&ctrl,NULL);  
      printf("press enter to continue\n");
      getchar();
      PLOT_close(draw,&ctrl);
      PLOT_free(draw,&ctrl);
      break;
    }    
    printf("------------------------------------------------------------------------------------------------------\n");
    printf("| %98s |\n","Timing log file options");
    printf("------------------------------------------------------------------------------------------------------\n");
    printf("| %2s | %20s | %70s |\n","","option","value");
    printf("------------------------------------------------------------------------------------------------------\n");
    if(data == NULL)
      printf("| %2d | %20s | %70s |\n",1,"Statistics output",ctrl.stat_file);
    printf("| %2d | %20s | %70s |\n",2,"Plots output",ctrl.plot_file);
    printf("| %2d | %20s | %70s |\n",3,"X values",label[x]);
    printf("| %2d | %20s | %70s |\n",4,"Y values",label[y]);
    printf("------------------------------------------------------------------------------------------------------\n");
    printf("| %2d | %93s |\n",5,"Back");
    printf("| %2d | %93s |\n",6,"Run");
    printf("------------------------------------------------------------------------------------------------------\n");
    printf("choice : ");
  }while(scan_choice(&choice) && choice != 5);

  printf("\n");
  
  if(data != NULL)
    LD_free(data);
  fclose(fp);
  return 1;
}
int logfile(logFinfo* info,int et){
  FILE *fp;
  plot *draw = NULL;
  logF *data = NULL;
  StatType **stat = NULL;
  ctrl ctrl;
  double *m_stat = NULL, *zm_stat = NULL;
  double **i_stat = NULL, **zi_stat = NULL;
  char filename[MAX_S];
  int nb=0, methods[MAX_M];

  int x, y, z, choice = -1, choice2 = -1;
  char label[76][MAX_S], stat_file[MAX_S], stdout_file[MAX_S], plot_file[MAX_S];
  char file[76][MAX_S];

  int matching=0,zmatching=0,group;
  int x_type=1,x_value=1,z_type=1,z_value=1;

  char* plotstyle;
  char distribution[MAX_S],xy[MAX_S],xyz[MAX_S],percent[MAX_S];
  int nb_d,nb_plots;
  double **d_x;
  char **plot_names = NULL;
  char **group_names = NULL;
  char *partition,before_ini[MAX_S],after_ini[MAX_S];

  int i;
  char **mincew_match,**maxcew_match,**mintcc_match,**maxtcc_match;
  char **minim_match,**maxim_match;
  double *mincew_val,*maxcew_val,*mintcc_val,*maxtcc_val;
  double *minim_val,*maxim_val;

  int **match_ids;
  double mininicew_val;
  int mininicew_id;
  double maxinicew_val;
  int maxinicew_id;

  /* initialization */
  if(et == AFTER_ET){
    strcpy(filename,info->base);
    strcat(filename,".log");
  }else{
    strcpy(filename,info->base);
    strcat(filename,".bet.log");
  }

  WARNING(((fp = fopen(filename,"r")) == NULL,
	   "logfile error code 1 : Cannot open file %s\n",filename),1);

  /* labels */
  strcpy(before_ini,"before initial partitioning");
  strcpy(after_ini,"after initial partitioning");
  strcpy(percent,"percent");
  strcpy(distribution,"plot distribution");
  strcpy(xy,"plot xy graph");
  strcpy(xyz,"plot 3d graph");

  strcpy(label[LOG_CUTEDGE], "Cut Edge Weight");
  strcpy(label[LOG_TOTCOMMCOST], "Total Communication Cost");
  strcpy(label[LOG_MAXWEIGHT], "Maximum Processor Weight");
  strcpy(label[LOG_CUTEDGEBYTOTCOMMCOST], "Cut Edge Weight / Total Communication Cost");
  strcpy(label[LOG_IMBALANCE], "Imbalance Factor");
  strcpy(label[LOG_MAXEXECTIME], "Maximum Execution Time");
  strcpy(label[LOG_TOTEXECTIME], "Total Execution Time");
  strcpy(label[LOG_IMBEXECTIME], "Execution Time Imbalance");
  strcpy(label[11],"matching min degree");
  strcpy(label[12],"matching max degree");
  strcpy(label[13],"matching average degree");
  strcpy(label[14],"matching standard deviation of degrees");
  strcpy(label[15],"matching sum of degrees");
  strcpy(label[21],"matching min vertex weight");
  strcpy(label[22],"matching max vertex weight");
  strcpy(label[23],"matching average vertex weight");
  strcpy(label[24],"matching standard deviation of vertex weights");
  strcpy(label[25],"matching sum of vertex weights");
  strcpy(label[31],"matching min cut edge");
  strcpy(label[32],"matching max cut edge");
  strcpy(label[33],"matching average cut edge");
  strcpy(label[34],"matching standard deviation of cut edges");
  strcpy(label[35],"matching sum of cut edges");
  strcpy(label[40],"matching imbalance factor");
  strcpy(label[71],"matching min edge weight");
  strcpy(label[72],"matching max edge weight");
  strcpy(label[73],"matching average edge weight");
  strcpy(label[74],"matching standard deviation of edge weights");
  strcpy(label[75],"matching sum of edge weights");

  strcpy(file[LOG_CUTEDGE], ".cut");
  strcpy(file[LOG_TOTCOMMCOST], ".comm");
  strcpy(file[LOG_MAXWEIGHT], ".maxw");
  strcpy(file[LOG_CUTEDGEBYTOTCOMMCOST], ".cutbycomm");
  strcpy(file[LOG_IMBALANCE], ".im");
  strcpy(file[LOG_MAXEXECTIME], ".maxex");
  strcpy(file[LOG_TOTEXECTIME], ".totex");
  strcpy(file[LOG_IMBEXECTIME], ".imbex");
  strcpy(file[11],".mind");
  strcpy(file[12],".maxd");
  strcpy(file[13],".avgd");
  strcpy(file[14],".stdevd");
  strcpy(file[15],".sumd");
  strcpy(file[21],".minw");
  strcpy(file[22],".maxw");
  strcpy(file[23],".avgw");
  strcpy(file[24],".stdevw");
  strcpy(file[25],".sumw");
  strcpy(file[31],".minc");
  strcpy(file[32],".maxc");
  strcpy(file[33],".avgc");
  strcpy(file[34],".stdevc");
  strcpy(file[35],".sumc");
  strcpy(file[40],".imb");
  strcpy(file[71],".minew");
  strcpy(file[72],".maxew");
  strcpy(file[73],".avgew");
  strcpy(file[74],".stdevew");
  strcpy(file[75],".sumew");

  /* filenames */
  strcpy(stdout_file,"stdout");
  strcpy(stat_file,info->base);
  strcat(stat_file,".stat.log");

  /* options */
  /* default */
  ctrl.stat_out     = FILES;
  ctrl.stat_file    = stat_file;
  ctrl.plot_out     = STDOUT;
  ctrl.plot_file    = plot_file;
  strcpy(plot_file,stdout_file);
  plotstyle         = xy;
  partition         = before_ini;
  ctrl.multiple_win = NO;
  nb_d              = 10;
  
  x                 = LOG_CUTEDGE;
  y                 = LOG_MAXWEIGHT;
  z                 = LOG_CUTEDGE;

  matching          = 0;
  group             = BY_COARSENING;

  /* menu to choose options */
  do{
    switch(choice){
      /****************************************
       * changing parameters
       ****************************************/
      /* Statistics output */
    case 0:
      do{
	printf("-------------------------------------------------------------------------------\n");
	  printf("| %75s |\n","Plot Style");
	  printf("-------------------------------------------------------------------------------\n");
	  printf("| %2d | %70s |\n",1,distribution);
	  printf("| %2d | %70s |\n",2,xy);
	  printf("| %2d | %70s |\n",3,xyz);
	  printf("-------------------------------------------------------------------------------\n");
	  printf("choice : ");
      }while(!scan_choice(&choice2) && (choice2 != 1 || choice2 != 2));
      switch(choice2){
      case 1:
	plotstyle = distribution;
	ctrl.multiple_win = YES;
	if(ctrl.plot_out != STDOUT){
	  if(partition == before_ini)
	    sprintf(plot_file,".bi%s.%s",file[x],ext[ctrl.plot_out]);
	  else
	    sprintf(plot_file,".ai%s.%s",file[x],ext[ctrl.plot_out]);
	}
	break;
      case 2:
	plotstyle = xy;
	ctrl.multiple_win = NO;
	if(ctrl.plot_out != STDOUT){
	  if(partition == before_ini)
	    sprintf(plot_file,"%s.bi%s%s.%s",info->base,file[x],file[y],ext[ctrl.plot_out]);
	  else
	    sprintf(plot_file,"%s.ai%s%s.%s",info->base,file[x],file[y],ext[ctrl.plot_out]);
	}
	break;
      case 3:
	plotstyle = xyz;
	ctrl.multiple_win = NO;
	if(ctrl.plot_out != STDOUT){
	  if(partition == before_ini)
	    sprintf(plot_file,"%s.bi%s%s%s.%s",info->base,
		    file[x],file[y],file[z],ext[ctrl.plot_out]);
	  else
	    sprintf(plot_file,"%s.ai%s%s%s.%s",info->base,
		    file[x],file[y],file[z],ext[ctrl.plot_out]);
	}
	break;
      }
      break;
    case 1:
      if(data == NULL){
	do{
	  printf("-------------------------------------------------------------------------------\n");
	  printf("| %75s |\n","Statistics output");
	  printf("-------------------------------------------------------------------------------\n");
	  printf("| %2d | %70s |\n",1,stdout_file);
	  printf("| %2d | %70s |\n",2,stat_file);
	  printf("-------------------------------------------------------------------------------\n");
	  printf("choice : ");
	}while(!scan_choice(&choice2) && (choice2 != 1 || choice2 != 2));
	switch(choice2){
	case 1:
	  ctrl.stat_out = STDOUT;
	  ctrl.stat_file = stdout_file;
	  break;
	case 2:
	  ctrl.stat_out = FILES;
	  ctrl.stat_file = stat_file;
	  break;
	}
      }
      break;
      /* Plots output */
    case 2:
      do{
	printf("-------------------\n");
	printf("| %15s |\n","Plots output");
	printf("-------------------\n");
	printf("| %2d | %10s |\n",STDOUT,stdout_file);
	printf("| %2d | %10s |\n",PNG,"PNG");
	printf("| %2d | %10s |\n",PS,"PS");
	printf("| %2d | %10s |\n",EPS,"EPS");
	printf("| %2d | %10s |\n",PDF,"PDF");
	printf("-------------------\n");
	printf("choice : ");
      }while(!scan_choice(&choice2) && (choice2 != STDOUT || choice2 != PNG || choice2 != PS || choice2 != EPS || choice2 != PDF));
      ctrl.plot_out = choice2;
      if(plotstyle == xy){
	if(choice2 != STDOUT){
	  if(partition == before_ini)
	    sprintf(plot_file,"%s.bi%s%s.%s",info->base,file[x],file[y],ext[choice2]);
	  else
	    sprintf(plot_file,"%s.ai%s%s.%s",info->base,file[x],file[y],ext[choice2]);
	}else
	  strcpy(plot_file,stdout_file);
      }else{
	if(choice2 != STDOUT){
	  if(partition == before_ini)
	    sprintf(plot_file,".bi%s.%s",file[x],ext[choice2]);
	  else
	    sprintf(plot_file,".ai%s.%s",file[x],ext[choice2]);
	}else
	  strcpy(plot_file,stdout_file);
      }
      break;
      /* partition */
    case 3:
      do{
	printf("-------------------------------------------------------------------------------\n");
	  printf("| %75s |\n","Partition");
	  printf("-------------------------------------------------------------------------------\n");
	  printf("| %2d | %70s |\n",1,before_ini);
	  printf("| %2d | %70s |\n",2,after_ini);
	  printf("-------------------------------------------------------------------------------\n");
	  printf("choice : ");
      }while(!scan_choice(&choice2) && (choice2 != 1 || choice2 != 2));
      switch(choice2){
      case 1:
	partition = before_ini;
	if(x == 40) /* imbalance factor for x */
	  x = 35;
	break;
      case 2:
	partition = after_ini;
	break;
      }
      if(ctrl.plot_out != STDOUT){
	if(plotstyle == xy){
	  if(partition == before_ini)
	    sprintf(plot_file,"%s.bi%s%s.%s",info->base,file[x],file[y],ext[ctrl.plot_out]);
	  else
	    sprintf(plot_file,"%s.ai%s%s.%s",info->base,file[x],file[y],ext[ctrl.plot_out]);
	}else{
	  if(partition == before_ini)
	    sprintf(plot_file,".bi%s.%s",file[x],ext[ctrl.plot_out]);
	  else
	    sprintf(plot_file,".ai%s.%s",file[x],ext[ctrl.plot_out]);
	}
      }
      break;
      /* X values */
    case 4:
    case 5:
    case 6:
      do{
	printf("-------------------------------------------------------------------------------\n");
	if(choice == 4){
	  printf("| %75s |\n","X values");
	}else{
	  printf("| %75s |\n","Y values");
	}
	printf("-------------------------------------------------------------------------------\n");
	printf("| %2d | %70s |\n",LOG_CUTEDGE,label[LOG_CUTEDGE]);
	printf("| %2d | %70s |\n",LOG_TOTCOMMCOST,label[LOG_TOTCOMMCOST]);
	printf("| %2d | %70s |\n",LOG_MAXWEIGHT,label[LOG_MAXWEIGHT]);
	printf("| %2d | %70s |\n",LOG_CUTEDGEBYTOTCOMMCOST,label[LOG_CUTEDGEBYTOTCOMMCOST]);
	printf("| %2d | %70s |\n",LOG_IMBALANCE,label[LOG_IMBALANCE]);
	printf("| %2d | %70s |\n",LOG_MAXEXECTIME,label[LOG_MAXEXECTIME]);
	printf("| %2d | %70s |\n",LOG_TOTEXECTIME,label[LOG_TOTEXECTIME]);
	printf("| %2d | %70s |\n",LOG_IMBEXECTIME,label[LOG_IMBEXECTIME]);
	
	if((choice == 4 || choice == 6)  && 
	   (plotstyle == xy || plotstyle == xyz ||group == BY_COARSENING)){
	  printf("-------------------------------------------------------------------------------\n");
	  printf("| %75s |\n","Coarsest Graph");
	  printf("-------------------------------------------------------------------------------\n");
	  printf("| %2d | %36s%18s %15s |\n",11,"","min","degree");
	  printf("| %2d | %36s%18s %15s |\n",12,"","max","");
	  printf("| %2d | %36s%18s %15s |\n",13,"","average","");
	  printf("| %2d | %36s%18s %15s |\n",14,"","standard deviation","");
	  printf("| %2d | %36s%18s %15s |\n",15,"","sum","");
	  printf("| %2d | %36s%18s %15s |\n",21,"","min","vertex weight");
	  printf("| %2d | %36s%18s %15s |\n",22,"","max","");
	  printf("| %2d | %36s%18s %15s |\n",23,"","average","");
	  printf("| %2d | %36s%18s %15s |\n",24,"","standard deviation","");
	  printf("| %2d | %36s%18s %15s |\n",25,"","sum","");
	  printf("| %2d | %36s%18s %15s |\n",31,"","min","cut edge");
	  printf("| %2d | %36s%18s %15s |\n",32,"","max","");
	  printf("| %2d | %36s%18s %15s |\n",33,"","average","");
	  printf("| %2d | %36s%18s %15s |\n",34,"","standard deviation","");
	  printf("| %2d | %36s%18s %15s |\n",35,"","sum","");
	  if(partition == after_ini)
	    printf("| %2d | %36s%8s %25s |\n",40,"","","imbalance factor");
	  printf("| %2d | %36s%18s %15s |\n",71,"","min","edge weight");
	  printf("| %2d | %36s%18s %15s |\n",72,"","max","");
	  printf("| %2d | %36s%18s %15s |\n",73,"","average","");
	  printf("| %2d | %36s%18s %15s |\n",74,"","standard deviation","");
	  printf("| %2d | %36s%18s %15s |\n",75,"","sum","");

	}
	printf("-------------------------------------------------------------------------------\n");
	printf("choice : ");
      }while(!scan_choice(&choice2) && 
	     (choice2 != LOG_CUTEDGE || choice2 != LOG_TOTCOMMCOST || 
	      choice2 != LOG_MAXWEIGHT || choice2 != LOG_CUTEDGEBYTOTCOMMCOST || 
	      choice2 != LOG_IMBALANCE || choice2 != LOG_MAXEXECTIME || 
	      choice2 != LOG_TOTEXECTIME || choice2 != LOG_IMBEXECTIME ||
	      (((choice == 6 || choice == 4) && 
		(plotstyle == xyz ||  plotstyle == xy ||  group == BY_COARSENING)) && 
	       (choice2 != 11 || choice2 != 12 || choice2 != 13 || choice2 != 14 || 
		choice2 != 15 || choice2 != 21 || choice2 != 22 || choice2 != 23 || 
		choice2 != 24 || choice2 != 25 || choice2 != 31 || choice2 != 32 || 
		choice2 != 33 || choice2 != 34 || choice2 != 35 || choice2 != 71 || 
		choice2 != 72 || choice2 != 73 || choice2 != 74 || choice2 != 75 || 
		(partition == after_ini && choice2 == 40)))));
      if(choice == 4){
	matching = 0;
	x = choice2;
	if((int)x/10 != 0){
	  matching = 1;
	  x_type = (int)x/10;
	  x_value = x % 10;
	  matching = 1;
	}
      }else if(choice == 6){
	zmatching = 0;
	z = choice2;
	if((int)z/10 != 0){
	  zmatching = 1;
	  z_type = (int)z/10;
	  z_value = z % 10;
	  zmatching = 1;
	}
      }else{
	y = choice2;
      }

      if(ctrl.plot_out != STDOUT){
	if(plotstyle == xy){
	  if(partition == before_ini)
	    sprintf(plot_file,"%s.bi%s%s.%s",info->base,file[x],file[y],ext[ctrl.plot_out]);
	  else
	    sprintf(plot_file,"%s.ai%s%s.%s",info->base,file[x],file[y],ext[ctrl.plot_out]);
	}else{
	  if(partition == before_ini)
	    sprintf(plot_file,".bi%s.%s",file[x],ext[ctrl.plot_out]);
	  else
	    sprintf(plot_file,".ai%s.%s",file[x],ext[ctrl.plot_out]);
	}
      }
      break;
    case 7:
      printf("Enter number of bars : ");
      while(!scan_choice(&choice2)){};
      nb_d = choice2;
      break;
    case 8:
      do{
	printf("-----------------------------\n");
	printf("| %25s |\n","Grouping method");
	printf("-----------------------------\n");
	printf("| %2d | %20s |\n",BY_MATCHING,"by matching file");
	printf("| %2d | %20s |\n",BY_COARSENING,"by coarsening method");
	printf("-----------------------------\n");
	printf("choice : ");
      }while(!scan_choice(&choice2) && (choice2 != BY_MATCHING || choice2 != BY_COARSENING));
      group = choice2;
      break;
      /****************************************
       * Running with parameters
       ****************************************/
    case 10:
      if(data == NULL){
	if(et == AFTER_ET)
	  data = LD_readLog(fp,&ctrl);
	else
	  data = LD_readBetLog(fp,&ctrl);
	fclose(fp);
	match_ids = U_malloc(data->c,int *,"logfile error code 2\n");

	for(i=0;i<data->c;i++)
	  match_ids[i] = LD_getMatchingIds(data, i);

	stat = MD_readStats(data->coarse,data->c,NULL,match_ids,info->F_graph,info->F_grid);
	
	LD_logBestByMethod(data,LOG_CUTEDGE,LD_MIN,&mincew_match,&mincew_val,NULL);
	LD_logBestByMethod(data,LOG_CUTEDGE,LD_MAX,&maxcew_match,&maxcew_val,NULL);
	LD_logBestByMethod(data,LOG_TOTCOMMCOST,LD_MIN,&mintcc_match,&mintcc_val,NULL);
	LD_logBestByMethod(data,LOG_TOTCOMMCOST,LD_MAX,&maxtcc_match,&maxtcc_val,NULL);
	LD_logBestByMethod(data,LOG_IMBALANCE,LD_MIN,&minim_match,&minim_val,NULL);
	LD_logBestByMethod(data,LOG_IMBALANCE,LD_MAX,&maxim_match,&maxim_val,NULL);
	
	if(stat != NULL){
	  MD_BestByMethod(stat[0]->ini,CUTEDGE,SUM,MIN,&mininicew_val,&mininicew_id);
	  MD_BestByMethod(stat[0]->ini,CUTEDGE,SUM,MAXI,&maxinicew_val,&maxinicew_id);	
	}else{
	  mininicew_val = maxinicew_val = -1;
	  mininicew_id  = maxinicew_id  = -1;
	}
	
      }

      for(i=0;i<=data->m;i++){
	if(i<data->m)
	  printf("%s :\n",data->methods[i]);
	else
	  printf("overall :\n");
	printf("-----------------------------------------------\n");
	printf("best  matching is %30s (min   cutedge %d)\n",(mincew_match[i]?mincew_match[i]:"not serialized"),(int)mincew_val[i]);
	printf("worst matching is %30s (max   cutedge %d)\n",(maxcew_match[i]?maxcew_match[i]:"not serialized"),(int)maxcew_val[i]);
	printf("best  matching is %30s (min       tcc %d)\n",(mintcc_match[i]?maxcew_match[i]:"not serialized"),(int)mintcc_val[i]);
	printf("worst matching is %30s (max       tcc %d)\n",(maxtcc_match[i]?maxtcc_match[i]:"not serialized"),(int)maxtcc_val[i]);
	printf("best  matching is %30s (min imbalance %f)\n",(minim_match[i]?minim_match[i]:"not serialized"),minim_val[i]);
	printf("worst matching is %30s (max imbalance %f)\n",(maxim_match[i]?maxim_match[i]:"not serialized"),maxim_val[i]);
      }
      printf("best        id is %30d (min inicutedge %d)\n",mininicew_id,(int)mininicew_val);
      printf("worst       id is %30d (min inicutedge %d)\n",maxinicew_id,(int)maxinicew_val);
      
      
      while(1){
	switch(group){
	case BY_COARSENING:
	  nb = getMethods(methods,data);
	  group_names = data->methods;
	  break;
	case BY_MATCHING:
	  nb = getMatchings(methods,data,mincew_match[data->m],maxcew_match[data->m]);
	  group_names = data->coarse;
	  break;
	}
	if(nb == 0)
	  break;

	/* getting matching info */
	if(matching == 1){
	  if(partition == before_ini){
	    m_stat = MD_getDataArray(stat,data->c,x_type,x_value);
	  }else{
	    i_stat = U_malloc(nb,double*,"logfile error code 3\n");
	    for(i=0;i<nb;i++){
	      switch(group){
	      case BY_COARSENING:
		i_stat[i] = MD_getIniDataArray(stat,
					       data->s[methods[i]],x_type,x_value,
					       data->id[methods[i]]);
		break;
	      case BY_MATCHING:
		i_stat[i] = MD_getIniDataArray(stat,
					       match_ids[methods[i]][0],x_type,x_value,
					       &(match_ids[methods[i]][1]));
		break;
	      }
	    }
	  }
	}

	/* getting y matching info */
	if(zmatching == 1){
	  if(partition == before_ini){
	    zm_stat = MD_getDataArray(stat,data->c,z_type,z_value);
	  }else{
	    zi_stat = U_malloc(nb,double*,"logfile error code 4\n");
	    for(i=0;i<nb;i++){
	      switch(group){
	      case BY_COARSENING:
		zi_stat[i] = MD_getIniDataArray(stat,
						data->s[methods[i]],z_type,z_value,
						data->id[methods[i]]);
		break;
	      case BY_MATCHING:
		zi_stat[i] = MD_getIniDataArray(stat,
						match_ids[methods[i]][0],z_type,z_value,
						&(match_ids[methods[i]][1]));
		break;
	      }
	    }
	  }
	}
	
	/* xy plots */
	if(plotstyle ==  xy){
	  /* using matching data */
	  if(matching == 1){
	    if(partition == before_ini){
	      draw = LD_getPlotXYmatchX(data,y,m_stat,methods,nb,group,label[x],label[y],"Spread",NO_BORDER,GRID,KEY,XTICS,YTICS);
	    }else{
	      draw = LD_getPlotXYiniX(data,y,i_stat,methods,nb,group,label[x],label[y],"Spread",NO_BORDER,GRID,KEY,XTICS,YTICS);
	    }
	  /* not using matching data */
	  }else{
	    draw = LD_getPlotXY(data,x,y,methods,nb,group,label[x],label[y],"Spread",NO_BORDER,GRID,KEY,XTICS,YTICS);
	  }
	/* xyz plots */
	}else if(plotstyle == xyz){
	  if(matching == 1 && zmatching ==0){
	    if(partition == before_ini){
	      draw = LD_getPlotXYZmatchX(data,z,y,m_stat,methods,nb,group,label[x],label[z],label[y],"3D Spread",NO_BORDER,GRID,KEY,XTICS,YTICS);
	    }else{
	      draw = LD_getPlotXYZiniX(data,z,y,i_stat,methods,nb,group,label[x],label[z],label[y],"3D Spread",NO_BORDER,GRID,KEY,XTICS,YTICS);
	    }
	  }else if(matching == 1 && zmatching ==1){
	    if(partition == before_ini){
	      draw = LD_getPlotXYZmatchXY(data,y,m_stat,zm_stat,methods,nb,group,label[x],label[z],label[y],"3D Spread",NO_BORDER,GRID,KEY,XTICS,YTICS);
	    }else{
	      draw = LD_getPlotXYZiniXY(data,y,i_stat,zi_stat,methods,nb,group,label[x],label[z],label[y],"3D Spread",NO_BORDER,GRID,KEY,XTICS,YTICS);
	    }
	  }else{
	    draw = LD_getPlotXYZ(data,x,z,y,methods,nb,group,label[x],label[z],label[y],"3D Spread",NO_BORDER,GRID,KEY,XTICS,YTICS);
	  }
	/* distribution */
	}else{
	  if(matching == 1){
	    printf("getDataMatch\n");
	    d_x = LD_getDataMatch(data,m_stat,methods,nb,group,&nb_plots);
	  }else{
	    printf("getData\n");
	    d_x = LD_getData(data,x,methods,nb,group,&nb_plots);
	  }
	  plot_names = U_malloc(nb_plots,char *,"logfile error code 5\n");
	  for(i=0;i<nb_plots;i++){
	    plot_names[i] = group_names[methods[i]];
	  }
	 
	  draw = PLOT_getDistribution(d_x,nb_d,group_names,methods,nb_plots,label[x],percent,"Distribution",NO_BORDER,GRID,KEY,XTICS,YTICS);
	  //for(i=0;i<data->m;i++)
	  for(i=0;i<nb;i++)
	    U_free("logfile error code 6\n",&(d_x[i]),LEND);
	  U_free("logfile error code 7\n",&d_x,LEND);
	}
	PLOT_draw(draw,&ctrl,plot_names);

	/* freeing matching info */
	if(matching == 1){
	  if(partition == before_ini){
	    U_free("logfile error code 8\n",&m_stat,LEND);
	  }else{
	    for(i=0;i<nb;i++)
	      U_free("logfile error code 9\n",&(i_stat[i]),LEND);
	    U_free("logfile error code 10\n",&i_stat,LEND);
	  }
	}
	if(zmatching == 1){
	  if(partition == before_ini){
	    U_free("logfile error code 11\n",&zm_stat,LEND);
	  }else{
	    for(i=0;i<nb;i++)
	      U_free("logfile error code 12\n",&(zi_stat[i]),LEND);
	    U_free("logfile error code 13\n",&zi_stat,LEND);
	  }
	}

	/* cleaning */
	U_free("logfile error code 14\n",&plot_names,LEND);
	printf("press enter to continue\n");
	getchar();
	PLOT_close(draw,&ctrl);
	PLOT_free(draw,&ctrl);
      }
      break;
    }
    /* print menu */
    printf("------------------------------------------------------------------------------------------------------\n");
    printf("| %98s |\n","Before execution time load balancing log file options");
    printf("------------------------------------------------------------------------------------------------------\n");
    printf("| %2s | %20s | %70s |\n","","option","value");
    printf("------------------------------------------------------------------------------------------------------\n");
    printf("| %2d | %20s | %70s |\n",0,"Style",plotstyle);
    if(data == NULL)
      printf("| %2d | %20s | %70s |\n",1,"Statistics output",ctrl.stat_file);
    printf("| %2d | %20s | %70s |\n",2,"Plots output",ctrl.plot_file);
    printf("| %2d | %20s | %70s |\n",3,"partition",partition);
    if(plotstyle == xy){
      printf("| %2d | %20s | %70s |\n",4,"X values",label[x]);
      printf("| %2d | %20s | %70s |\n",5,"Y values",label[y]);
    }else if(plotstyle == xyz){
      printf("| %2d | %20s | %70s |\n",4,"X values",label[x]);
      printf("| %2d | %20s | %70s |\n",5,"Z values",label[y]);
      printf("| %2d | %20s | %70s |\n",6,"Y values",label[z]);
    }else{
      printf("| %2d | %20s | %70s |\n",4,"values",label[x]);
      printf("| %2d | %20s | %70d |\n",7,"number of bars",nb_d);
    }
    printf("| %2d | %20s | %70s |\n",8,"Group",(group == BY_MATCHING ? "by matching file":"by coarsening method"));
    printf("------------------------------------------------------------------------------------------------------\n");
    printf("| %2d | %93s |\n",9,"Back");
    printf("| %2d | %93s |\n",10,"Run");
    printf("------------------------------------------------------------------------------------------------------\n");
    printf("choice : ");
  }while(scan_choice(&choice) && choice != 9);
  printf("\n");
  
  for(i=0;i<data->c;i++)
    U_free("logfile error code 15\n",&(match_ids[i]),LEND);
  U_free("logfile error code 16\n",&match_ids,LEND);

  if(data != NULL && stat != NULL)
    MD_freeArr(stat,data->c);
  
  if(data != NULL)
    LD_free(data);

  U_free("logfile error code 17\n",&mincew_match,&mincew_val,&maxcew_match,&maxcew_val,
	 &mintcc_match,&mintcc_val,&maxtcc_match,&maxtcc_val,&minim_match,
	 &maxim_match,&minim_val,&maxim_val,LEND);
  
      
  return 1;
}

int scan_choice(int *c){
  int result = 1;
  if(scanf("%d",c) != 1)
    result = 0;
  else
    getchar();
  return result;
}
int matchstats(logFinfo* info){
  int choice = -1,choice2 = -1;
  char stdout_file[MAX_S],m_stat_file[MAX_S],logfile[MAX_S],logbetfile[MAX_S];
  char log_stat_file[MAX_S],logbet_stat_file[MAX_S];
  char *filename,*m_output,*l_output;
  logF *data = NULL;
  StatType **stat = NULL;
  FILE *fp = NULL , *out = NULL;
  ctrl ctrl;
  int nb,methods[MAX_M];
  int i,j,k=0;

  Graph **graphs; /* array of pointers to matchings */
  int x,t,nb_bars;
  char label[20][MAX_S], file[20][MAX_S], plot_file[MAX_S], **plot_names;
  char tmp_plot_file[256];
  int nb_values, *id_plot_names;
  double **values;
  plot **draws = NULL;
  plot *draw = NULL;

  double **xlevels;
  char ylabel[256];

  char **mincew_match,**maxcew_match,**mintcc_match,**maxtcc_match;
  double *mincew_val,*maxcew_val,*mintcc_val,*maxtcc_val;

  int **match_ids;

  /* filenames */
  strcpy(stdout_file,"stdout");
  strcpy(m_stat_file,info->base);
  strcat(m_stat_file,".matching.stat.log");
  strcpy(logfile,info->base);
  strcpy(log_stat_file,info->base);
  strcat(logfile,".log");
  strcat(log_stat_file,".stat.log");
  strcpy(logbetfile,info->base);
  strcpy(logbet_stat_file,info->base);
  strcat(logbetfile,".bet.log");
  strcat(logbet_stat_file,".stat.bet.log");

  /* labels */
  strcpy(file[DEGREE],".vdeg");
  strcpy(file[WEIGHT],".vwgt");
  strcpy(file[CUTEDGE],".cedge");
  strcpy(file[NB_EDGES],".nbedges");
  strcpy(file[NB_VERTICES],".nbvtxs");
  strcpy(file[E_WEIGHT],".ewgt");
  strcpy(file[10 + MIN],".min");
  strcpy(file[10 + MAXI],".max");
  strcpy(file[10 + AVG],".avg");
  strcpy(file[10 + STDDEV],".stddev");
  strcpy(file[10 + SUM],".sum");

  /* labels */
  strcpy(label[DEGREE],"vertices degree");
  strcpy(label[WEIGHT],"vertices weight");
  strcpy(label[CUTEDGE],"vertices cut edge");
  strcpy(label[NB_EDGES],"edges number");
  strcpy(label[NB_VERTICES],"vertices number");
  strcpy(label[E_WEIGHT],"edge weights");
  strcpy(label[10 + MIN],"minimum");
  strcpy(label[10 + MAXI],"maximum");
  strcpy(label[10 + AVG],"average");
  strcpy(label[10 + STDDEV],"standard deviation");
  strcpy(label[10 + SUM],"sum");

  /* default option */
  m_output = m_stat_file;
  filename = logfile;
  l_output = log_stat_file;
  x = CUTEDGE;
  t = AVG;
  strcpy(plot_file,stdout_file);
  ctrl.plot_out = STDOUT;
  ctrl.plot_file = plot_file;
  ctrl.multiple_win = YES;
  nb_bars = 10;

  do{
    switch(choice){
      /******************************************
       * change options
       ******************************************/
    case 1:
      do{
	printf("-------------------------------------------------------------------------------\n");
	printf("| %75s |\n","Statistics output");
	printf("-------------------------------------------------------------------------------\n");
	printf("| %2d | %70s |\n",1,stdout_file);
	printf("| %2d | %70s |\n",2,m_stat_file);
	printf("-------------------------------------------------------------------------------\n");
	printf("choice : ");
      }while(!scan_choice(&choice2) && (choice2 != 1 || choice2 != 2));
      switch(choice2){
      case 1:
	m_output = stdout_file;
	break;
      case 2:
	m_output = m_stat_file;
	break;
      }
      break;
      /* choose log file */
    case 2:
      if(data == NULL){
	do{
	  printf("-------------------------------------------------------------------------------\n");
	  printf("| %75s |\n","Log file");
	  printf("-------------------------------------------------------------------------------\n");
	  printf("| %2d | %70s |\n",1,logfile);
	  printf("| %2d | %70s |\n",2,logbetfile);
	  printf("-------------------------------------------------------------------------------\n");
	  printf("choice : ");
	}while(!scan_choice(&choice2) && (choice2 != 1 || choice2 != 2));
	switch(choice2){
	case 1:
	  filename = logfile;
	  if(l_output != stdout_file)
	    l_output = log_stat_file;
	  break;
	case 2:
	  filename = logbetfile;
	  if(l_output != stdout_file)
	    l_output = logbet_stat_file;
	  break;
	}
      }
      break;
    case 3:
      if(data == NULL && 0){
	do{
	  printf("-------------------------------------------------------------------------------\n");
	  printf("| %75s |\n","Logfile stat output");
	  printf("-------------------------------------------------------------------------------\n");
	  printf("| %2d | %70s |\n",1,stdout_file);
	  if(filename == logfile)
	    printf("| %2d | %70s |\n",2,log_stat_file);
	  else
	    printf("| %2d | %70s |\n",2,logbet_stat_file);
	  printf("-------------------------------------------------------------------------------\n");
	  printf("choice : ");
	}while(!scan_choice(&choice2) && (choice2 != 1 || choice2 != 2));
	switch(choice2){
	case 1:
	  l_output = stdout_file;
	  break;
	case 2:
	  if(filename == logfile)
	    l_output = log_stat_file;
	  else
	    l_output = logbet_stat_file;
	  break;
	}
      }
      break;
    case 4:
      do{
	printf("---------------------------------------\n");
	printf("| %35s |\n","values");
	printf("---------------------------------------\n");
	printf("| %2d | %30s |\n",DEGREE,label[DEGREE]);
	printf("| %2d | %30s |\n",WEIGHT,label[WEIGHT]);
	printf("| %2d | %30s |\n",CUTEDGE,label[CUTEDGE]);
	printf("| %2d | %30s |\n",NB_VERTICES,label[NB_VERTICES]);
	printf("| %2d | %30s |\n",NB_EDGES,label[NB_EDGES]);
	printf("| %2d | %30s |\n",E_WEIGHT,label[E_WEIGHT]);
	printf("---------------------------------------\n");
	printf("choice : ");
      }while(!scan_choice(&choice2) && (choice2 != DEGREE || choice2 != WEIGHT || choice2 != CUTEDGE || choice2 != NB_VERTICES || choice2 != NB_EDGES || choice2 != E_WEIGHT));
      x = choice2;
      if(ctrl.plot_out != STDOUT)
	sprintf(plot_file,"%s.%s",file[x],ext[ctrl.plot_out]);
      break;
    case 6:
      printf("Enter number of bars : ");
      while(!scan_choice(&choice2)){};
      nb_bars = choice2;
      break;
    case 5:
      do{
	printf("-------------------\n");
	printf("| %15s |\n","Plots output");
	printf("-------------------\n");
	printf("| %2d | %10s |\n",STDOUT,stdout_file);
	printf("| %2d | %10s |\n",PNG,"PNG");
	printf("| %2d | %10s |\n",PS,"PS");
	printf("| %2d | %10s |\n",EPS,"EPS");
	printf("| %2d | %10s |\n",PDF,"PDF");
	printf("-------------------\n");
	printf("choice : ");
      }while(!scan_choice(&choice2) && (choice2 != STDOUT || choice2 != PNG || choice2 != PS || choice2 != EPS || choice2 != PDF));
      ctrl.plot_out = choice2;
      if(choice2 != STDOUT)
	sprintf(plot_file,"%s.%s",file[x],ext[choice2]);
      else
	strcpy(plot_file,stdout_file);
      break;
      /******************************************
       * run with options
       ******************************************/
    case 7:
      do{
	printf("-----------------------------\n");
	printf("| %25s |\n","Stats");
	printf("-----------------------------\n");
	printf("| %2d | %20s |\n",MIN,label[10 + MIN]);
	printf("| %2d | %20s |\n",MAXI,label[10 + MAXI]);
	printf("| %2d | %20s |\n",AVG,label[10 + AVG]);
	printf("| %2d | %20s |\n",STDDEV,label[10 + STDDEV]);
	printf("| %2d | %20s |\n",SUM,label[10 + SUM]);
	printf("-----------------------------\n");
	printf("choice : ");
      }while(!scan_choice(&choice2) && (choice2 != MIN || choice2 != MAXI || choice2 != AVG || choice2 != STDDEV || choice2 != SUM));
      t = choice2;
      break;
    case 9:
    case 10:
    case 11:
      if(data == NULL){
	if(l_output == stdout_file){
	  ctrl.stat_out = STDOUT;
	  ctrl.stat_file = NULL;
	}else{
	  ctrl.stat_out = FILES;
	  ctrl.stat_file = l_output;
	}
	WARNING(((fp = fopen(filename,"r")) == NULL,"matchstats error code 1 : Cannot open file %s\n",filename),1);
	if(filename == logfile){
	  data = LD_readLog(fp,&ctrl);
	}else if(filename == logbetfile)
	  data = LD_readBetLog(fp,&ctrl);
	fclose(fp);

	if(data->c == 0){
	  LD_free(data);
	  WARNING((1,"No serialize info\n"),0);
	}
	
	
	match_ids = U_malloc(data->c,int *,"logfile error code 1\n");
	for(i=0;i<data->c;i++)
	  match_ids[i] = LD_getMatchingIds(data, i);
	stat = MD_readStats(data->coarse,data->c,NULL,match_ids,info->F_graph,info->F_grid);
	
	LD_logBestByMethod(data,LOG_CUTEDGE,LD_MIN,&mincew_match,&mincew_val,NULL);
	LD_logBestByMethod(data,LOG_CUTEDGE,LD_MAX,&maxcew_match,&maxcew_val,NULL);
	LD_logBestByMethod(data,LOG_TOTCOMMCOST,LD_MIN,&mintcc_match,&mintcc_val,NULL);
	LD_logBestByMethod(data,LOG_TOTCOMMCOST,LD_MAX,&maxtcc_match,&maxtcc_val,NULL);
	
	
      }
      for(i=0;i<=data->m;i++){
	if(i<data->m)
	  printf("%s :\n",data->methods[i]);
	else
	  printf("overall :\n");
	printf("-----------------------------------------------\n");
	printf("best  matching is %30s (min cutedge %5d)\n",(mincew_match[i]?mincew_match[i]:"not serialized"),(int)mincew_val[i]);
	printf("worst matching is %30s (max cutedge %5d)\n",(maxcew_match[i]?maxcew_match[i]:"not serialized"),(int)maxcew_val[i]);
	printf("best  matching is %30s (min     tcc %5d)\n",(mintcc_match[i]?mintcc_match[i]:"not serialized"),(int)mintcc_val[i]);
	printf("worst matching is %30s (max     tcc %5d)\n",(maxtcc_match[i]?maxtcc_match[i]:"not serialized"),(int)maxtcc_val[i]);
      }
      while((nb = getMatchings(methods,data,mincew_match[data->m],maxcew_match[data->m])) != 0){
	/* printing */
	if(choice == 9){
	  if(m_output == stdout_file){
	    out = stdout;
	  }else if(m_output == m_stat_file){
	    WARNING(((out = fopen(m_output,"w")) == NULL,"matchstats error code 2 : cannot open %s\n",m_output),0);
	  }
	  for(i=0;i<nb;i++){
	    /* pick one id can't print all */
	    for(j=0;j<data->m;j++){
	      for(k=0;k<data->s[j];k++)
		if(data->matching[j][k] == methods[i])
		  goto ID_PICKED1;
	    }
	  ID_PICKED1:
	    MD_printMatchingStats(stat[methods[i]],out,data->id[j][k]);
	  }
	  
	  if(m_output != stdout_file)
	    fclose(out);
	  
	  printf("press enter to continue\n");
	  getchar();
	/* plotting */
	}else if(choice == 10){
	  /* getting graphs */
	  graphs = MD_getGraphs(data->coarse,methods,nb);
	  /* distribution */
	  draws = U_malloc(nb,plot *,"matchstats error code 2\n");
	  for(i=0;i<nb;i++){
	    values = MD_getGraphData(graphs[i],x,data->coarse[methods[i]],
				     &plot_names,&nb_values);
	    /*printf("%d \n",nb_values);
	      for(j=0;j<nb_values;j++){
	      printf("%f \n",values[j][0]);
	      }*/
	    id_plot_names = U_malloc(nb_values,int,"matchstats error code 3\n");
	    for(j=0;j<nb_values;j++){
	      id_plot_names[j]=j;
	    }
	    draws[i] = PLOT_getDistribution(values,nb_bars,plot_names,id_plot_names,nb_values,label[x],"percent","distribution",NO_BORDER,GRID,KEY,XTICS,YTICS);
	    PLOT_draw(draws[i],&ctrl,plot_names);
	    for(i=0;i<nb_values;i++)
	      U_free("matchstats error code 4\n",&(values[i]));
	    U_free("matchstats error code 5\n",&plot_names,&values,&id_plot_names,LEND);    
	  }
	  printf("press enter to continue\n");
	  getchar();
	  for(i=0;i<nb;i++){
	    PLOT_close(draws[i],&ctrl);
	    PLOT_free(draws[i],&ctrl);
	    MD_freeGraph(graphs[i]);
	  }
	  U_free("matchstats error code 6\n",&graphs,&draws,LEND);
	  /* plot evolution */
	}else{
	  values = U_malloc(nb,double *,"matchstats error code 7\n");
	  xlevels = U_malloc(nb,double *,"matchstats error code 8\n");
	  for(i=0;i<nb;i++){
	    /* pick one id can't print all */
	    for(j=0;j<data->m;j++){
	      for(k=0;k<data->s[j];k++)
		if(data->matching[j][k] == methods[i])
		  goto ID_PICKED2;
	    }
	    
	    /* id picked */
	  ID_PICKED2:
	    values[i] = MD_getLevelData(stat[methods[i]],x,t,data->id[j][k]);
	    xlevels[i] = U_malloc(stat[methods[i]]->levels + 2,double,"matchstats error code 9\n");
	    xlevels[i][0] = stat[methods[i]]->levels + 1;
	    for(j=0;j<=stat[methods[i]]->levels;j++)
	      xlevels[i][j+1] = j;      
	  }
	  sprintf(ylabel,"%s %s",label[x],label[10+t]);
	  draw = PLOT_xy(xlevels,values,data->coarse,methods,nb,"level",ylabel,"Evolution",NO_BORDER,GRID,KEY,XTICS,YTICS);
	  ctrl.multiple_win = NO;
	  
	  /* file name */
	  if(ctrl.plot_out != STDOUT){
	    strcpy(tmp_plot_file,plot_file);
	    sprintf(plot_file,"%s%s%s",info->base,file[10+t],tmp_plot_file);
	  }
	  PLOT_draw(draw,&ctrl,NULL);
	  printf("press enter to continue\n");
	  getchar();
	  PLOT_close(draw,&ctrl);
	  PLOT_free(draw,&ctrl);
	  for(i=0;i<nb;i++){
	    U_free("matchstats error code 10\n",&(values[i]),&(xlevels[i]),LEND);
	  }
	  U_free("matchstats error code 11\n",&values,&xlevels,LEND);
	  ctrl.multiple_win = YES;
	  if(ctrl.plot_out != STDOUT)
	    strcpy(plot_file,tmp_plot_file);
	}
      }
      break;
    }
    printf("-----------------------------------------------------------------------------------------------------------\n");
    printf("| %103s |\n","Matching statistics");
    printf("-----------------------------------------------------------------------------------------------------------\n");
    printf("| %2s | %25s | %70s |\n","","option","value");
    printf("-----------------------------------------------------------------------------------------------------------\n");
    printf("| %2d | %25s | %70s |\n",1,"matching output",m_output);
    if(data == NULL){
      printf("| %2d | %25s | %70s |\n",2,"log file",filename);
      printf("| %2d | %25s | %70s |\n",3,"log file stats output",l_output);
    }
    
    printf("-----------------------------------------------------------------------------------------------------------\n");
    printf("| %103s |\n","Graphs");
    printf("-----------------------------------------------------------------------------------------------------------\n");
    printf("| %2d | %25s | %70s |\n",4,"values",label[x]);
    printf("| %2d | %25s | %70s |\n",5,"plot output",plot_file);
    printf("-----------------------------------------------------------------------------------------------------------\n");
    printf("-----------------------------------------------------------------------------------------------------------\n");
    printf("| %103s |\n","Distribution");
    printf("-----------------------------------------------------------------------------------------------------------\n");
    printf("| %2d | %25s | %70d |\n",6,"number of bars",nb_bars);
    printf("-----------------------------------------------------------------------------------------------------------\n");
    printf("| %103s |\n","Evolution");
    printf("-----------------------------------------------------------------------------------------------------------\n");
    printf("| %2d | %25s | %70s |\n",7,"stat",label[10 + t]);
    printf("-----------------------------------------------------------------------------------------------------------\n");
    printf("| %2d | %98s |\n",8,"back");
    printf("| %2d | %98s |\n",9,"print matching log file");
    printf("| %2d | %98s |\n",10,"plot distribution");
    printf("| %2d | %98s |\n",11,"plot evolution");
    printf("-----------------------------------------------------------------------------------------------------------\n");
    printf("choice : ");
  }while(scan_choice(&choice) && choice != 8);
  printf("\n");


  if(data != NULL){
    for(i=0;i<data->c;i++)
      U_free("logfile error code 9\n",&(match_ids[i]),LEND);
    U_free("logfile error code 10\n",&match_ids,LEND);

    MD_freeArr(stat,data->c);
    LD_free(data);
  }

  U_free("logfile error code 1\n",&mincew_match,&mincew_val,&maxcew_match,&maxcew_val,
	 &mintcc_match,&mintcc_val,&maxtcc_match,&maxtcc_val,LEND);
  
  return 1;
}
int dxoutput(logFinfo* info){
  int nb,i,methods[MAX_M];
  Graph **graphs;
  logF *data = NULL;
  StatType **stat = NULL;
  char filename[MAX_S];
  ctrl ctrl;
  FILE* fp;

  char **mincew_match,**maxcew_match,**mintcc_match,**maxtcc_match;
  double *mincew_val,*maxcew_val,*mintcc_val,*maxtcc_val;

  int **match_ids;

  /* initialization */
  strcpy(filename,info->base);
  strcat(filename,".bet.log");
  WARNING(((fp = fopen(filename,"r")) == NULL,"Cannot open file %s\n",filename),1);

  /* default options */
  ctrl.multiple_win = NO;
  ctrl.plot_out = STDOUT;
  ctrl.stat_out = STDOUT;
  ctrl.plot_file = NULL;
  ctrl.stat_file = NULL;



  data = LD_readBetLog(fp,&ctrl);
  match_ids = U_malloc(data->c,int *,"logfile error code 1\n");
  for(i=0;i<data->c;i++)
    match_ids[i] = LD_getMatchingIds(data, i);
  fclose(fp);
  stat = MD_readStats(data->coarse,data->c,NULL,match_ids,info->F_graph,info->F_grid);
  LD_logBestByMethod(data,LOG_CUTEDGE,LD_MIN,&mincew_match,&mincew_val,NULL);
  LD_logBestByMethod(data,LOG_CUTEDGE,LD_MAX,&maxcew_match,&maxcew_val,NULL);
  LD_logBestByMethod(data,LOG_TOTCOMMCOST,LD_MIN,&mintcc_match,&mintcc_val,NULL);
  LD_logBestByMethod(data,LOG_TOTCOMMCOST,LD_MAX,&maxtcc_match,&maxtcc_val,NULL);
	
	
  for(i=0;i<=data->m;i++){
    if(i<data->m)
      printf("%s :\n",data->methods[i]);
    else
      printf("overall :\n");
    printf("-----------------------------------------------\n");
    printf("best  matching is %30s (min cutedge %5d)\n",mincew_match[i],(int)mincew_val[i]);
    printf("worst matching is %30s (max cutedge %5d)\n",maxcew_match[i],(int)maxcew_val[i]);
    printf("best  matching is %30s (min     tcc %5d)\n",mintcc_match[i],(int)mintcc_val[i]);
    printf("worst matching is %30s (max     tcc %5d)\n",maxtcc_match[i],(int)maxtcc_val[i]);
  }
  
  while((nb = getMatchings(methods,data,mincew_match[data->m],maxcew_match[data->m])) != 0){
    graphs = MD_getGraphs(data->coarse,methods,nb);
    for(i=0;i<nb;i++){
      DX_matching(graphs[i],data->coarse[methods[i]],info->coords);
    }
    for(i=0;i<nb;i++){
      MD_freeGraph(graphs[i]);
    }
    U_free("refinedxoutput error code 6\n",&graphs,LEND);
  }

  for(i=0;i<data->c;i++)
    U_free("logfile error code 9\n",&(match_ids[i]),LEND);
  U_free("logfile error code 10\n",&match_ids,LEND);
  MD_freeArr(stat,data->c);
  LD_free(data);

  U_free("logfile error code 1\n",&mincew_match,&mincew_val,&maxcew_match,&maxcew_val,
	 &mintcc_match,&mintcc_val,&maxtcc_match,&maxtcc_val,LEND);

  return 1;
}
int refinestats(logFinfo* info){
  char filename[256];
  char log_file[256],plot_file[256];
  char stdout_file[256];
  int x,i,j,k;
  char label[27][256],file[27][256];
  ctrl ctrl;
  int choice = -1, choice2 = -1;
  
  int x_type,x_value,x_stat = 0;
  int *where;
  int *match;
  int max_w = 100, nb_w = 0;
  int nb = 0, methods[MAX_M];
  double **plot_x;
  double **plot_y;
  char **fields;
  int *fields_id;
  
  RefineStats **rstats = NULL;
  
  int *mincew_id,*maxcew_id,*mintcc_id,*maxtcc_id;
  char **mincew_match,**maxcew_match,**mintcc_match,**maxtcc_match;
  double *mincew_val,*maxcew_val,*mintcc_val,*maxtcc_val;


  logF *data = NULL;
  FILE *fp;

  plot* draw;

  /* init */
  where = U_malloc(max_w,int,"refinestats error code 1\n");
  match = U_malloc(max_w,int,"refinestats error code 2\n");
  strcpy(filename,info->base);
  strcat(filename,".bet.log");

  /* filename */
  strcpy(stdout_file,"stdout");
  

  /* labels */
  strcpy(label[R_CUTEDGE],"cutedge");
  strcpy(label[R_TCC],"total communication cost");
  strcpy(label[R_IMBALANCE],"imbalance");
  strcpy(label[R_MET],"maximum execution time");
  strcpy(label[R_MET025],"maximum execution time (ratio 0.25)");
  strcpy(label[R_MET05],"maximum execution time (ratio 0.5)");
  strcpy(label[R_MET1],"maximum execution time (ratio 1)");
  strcpy(label[R_MET2],"maximum execution time (ratio 2)");
  strcpy(label[11],"processor vertex weight min");
  strcpy(label[12],"processor vertex weight max");
  strcpy(label[13],"processor vertex weight average");
  strcpy(label[14],"processor vertex weight standard deviation");
  strcpy(label[15],"processor vertex weight sum");
  strcpy(label[21],"processor execution time min");
  strcpy(label[22],"processor execution time max");
  strcpy(label[23],"processor execution time average");
  strcpy(label[24],"processor execution time standard deviation");
  strcpy(label[25],"processor execution time sum");
  strcpy(label[26],"processor execution time imbalance");
  
  strcpy(file[R_CUTEDGE],".cew");
  strcpy(file[R_TCC],".tcc");
  strcpy(file[R_IMBALANCE],".im");
  strcpy(file[R_MET],".et");
  strcpy(file[R_MET025],".et025");
  strcpy(file[R_MET05],".et05");
  strcpy(file[R_MET1],".et1");
  strcpy(file[R_MET2],".et2");
  strcpy(file[11],".pbet.min");
  strcpy(file[12],".pbet.max");
  strcpy(file[13],".pbet.avg");
  strcpy(file[14],".pbet.stddev");
  strcpy(file[15],".pbet.sum");
  strcpy(file[21],".pet.min");
  strcpy(file[22],".pet.max");
  strcpy(file[23],".pet.avg");
  strcpy(file[24],".pet.stddev");
  strcpy(file[25],".pet.sum");
  strcpy(file[26],".pet.im");

  /* default */
  ctrl.multiple_win = NO;
  ctrl.plot_out = STDOUT;
  ctrl.plot_file = plot_file;
  strcpy(plot_file,stdout_file);

  ctrl.stat_out = STDOUT;
  ctrl.stat_file = log_file;
  strcpy(log_file,stdout_file);

  x = R_CUTEDGE;
  x_type = MIN;
  x_value = R_PROVW;
  do{
    switch(choice){
    case 2:
      do{
	printf("-------------------------------------------------------------------------------\n");
	printf("| %75s |\n","Y values");
	printf("-------------------------------------------------------------------------------\n");
	printf("| %2d | %70s |\n",R_CUTEDGE,label[R_CUTEDGE]);
	printf("| %2d | %70s |\n",R_TCC,label[R_TCC]);
	printf("| %2d | %70s |\n",R_IMBALANCE,label[R_IMBALANCE]);
	printf("| %2d | %70s |\n",R_MET,label[R_MET]);
	printf("| %2d | %70s |\n",R_MET025,label[R_MET025]);
	printf("| %2d | %70s |\n",R_MET05,label[R_MET05]);
	printf("| %2d | %70s |\n",R_MET1,label[R_MET1]);
	printf("| %2d | %70s |\n",R_MET2,label[R_MET2]);
	printf("-------------------------------------------------------------------------------\n");
	printf("| %2d | %16s%18s %35s |\n",11,"","min","processor vertices weight");
	printf("| %2d | %16s%18s %35s |\n",12,"","max","");
	printf("| %2d | %16s%18s %35s |\n",13,"","average","");
	printf("| %2d | %16s%18s %35s |\n",14,"","standard deviation","");
	printf("| %2d | %16s%18s %35s |\n",15,"","sum","");
	printf("| %2d | %16s%18s %35s |\n",21,"","min","processor execution time");
	printf("| %2d | %16s%18s %35s |\n",22,"","max","");
	printf("| %2d | %16s%18s %35s |\n",23,"","average","");
	printf("| %2d | %16s%18s %35s |\n",24,"","standard deviation","");
	printf("| %2d | %16s%18s %35s |\n",25,"","sum","");
	printf("| %2d | %16s%18s %35s |\n",26,"","imbalance","");
	printf("-------------------------------------------------------------------------------\n");
	printf("choice : ");
      }while(!scan_choice(&choice2) &&
	     (choice2 != R_CUTEDGE || choice2 != R_TCC || 
	      choice2 != R_IMBALANCE || choice2 != R_MET || 
	      choice2 != R_MET025 || choice2 != R_MET05 || 
	      choice2 != R_MET1 || choice2 != R_MET2 || choice2 != 11 || 
	      choice2 != 12 || choice2 != 13 || choice2 != 14 || 
	      choice2 != 15 || choice2 != 21 || choice2 != 22 || 
	      choice2 != 23 || choice2 != 24 || choice2 != 25 || 
	      choice2 != 26));
      x_stat = 0;
      x = choice2;
      if((int)x/10 != 0){
	x_value = (int)x/10;
	x_type = x % 10;
	x_stat = 1;
      }

      if(ctrl.plot_out != STDOUT){
	sprintf(plot_file,"%s%s.%s",info->base,file[x],ext[ctrl.plot_out]);
      }
      break;
    case 1:
      do{
	printf("-------------------\n");
	printf("| %15s |\n","Plots output");
	printf("-------------------\n");
	printf("| %2d | %10s |\n",STDOUT,stdout_file);
	printf("| %2d | %10s |\n",PNG,"PNG");
	printf("| %2d | %10s |\n",PS,"PS");
	printf("| %2d | %10s |\n",EPS,"EPS");
	printf("| %2d | %10s |\n",PDF,"PDF");
	printf("-------------------\n");
	printf("choice : ");
      }while(!scan_choice(&choice2) && (choice2 != STDOUT || choice2 != PNG || choice2 != PS || choice2 != EPS || choice2 != PDF));
      ctrl.plot_out = choice2;
      if(choice2 != STDOUT)
	sprintf(plot_file,"%s%s.%s",info->base,file[x],ext[choice2]);
      else
	strcpy(plot_file,stdout_file);
      break;
    case 4:
      if(rstats == NULL){
	WARNING(((fp = fopen(filename,"r")) == NULL,"refinestats error code 3 : cannot open %s\n",filename),0);
	data = LD_readBetLog(fp,&ctrl);
	fclose(fp);

	if(data->c == 0){
	  U_free("refinestats error code 4\n",&(where),&(match),LEND);
	  LD_free(data);
	  WARNING((1,"No serialize info\n"),0);
	}

	/* from data get matchings and where ids */
	for(i=0;i<data->m;i++){
	  for(j=0;j<data->s[i];j++){
	    /* rampage not a too big array (<1000) search the whole array 
	       each time */
	    for(k=0;k<nb_w;k++){
	      if(where[k] == data->id[i][j])
		break;
	    }
	    if(k == nb_w){
	      if(data->matching[i][j] == -1)
		continue;
	      where[k] = data->id[i][j];
	      match[k] = data->matching[i][j];
	      nb_w++;
	      if(nb_w >= max_w){
		max_w *= 2;
		where = U_realloc(where,max_w,int,"refinestats error code 4\n");
		match = U_realloc(match,max_w,int,"refinestats error code 4\n");
	      }
	    }
	  }
	}
	/* done with searching those ids */
	/* now lets find out those stats */
	rstats = MD_readRefineStats(data->coarse,match,where,info->base, nb_w,NULL);
	LD_logBestByMethod(data,LOG_CUTEDGE,LD_MIN,&mincew_match,&mincew_val,&mincew_id);
	LD_logBestByMethod(data,LOG_CUTEDGE,LD_MAX,&maxcew_match,&maxcew_val,&maxcew_id);
	LD_logBestByMethod(data,LOG_TOTCOMMCOST,LD_MIN,&mintcc_match,&mintcc_val,&mintcc_id);
	LD_logBestByMethod(data,LOG_TOTCOMMCOST,LD_MAX,&maxtcc_match,&maxtcc_val,&maxtcc_id);
      }
	
      for(i=0;i<=data->m;i++){
	if(i<data->m)
	  printf("%s :\n",data->methods[i]);
	else
	  printf("overall :\n");
	printf("-----------------------------------------------\n");
	printf("best  matching is %30s (min cutedge %5d)\n",(mincew_match[i]?mincew_match[i]:"not serialized"),(int)mincew_val[i]);
	printf("worst matching is %30s (max cutedge %5d)\n",(maxcew_match[i]?maxcew_match[i]:"not serialized"),(int)maxcew_val[i]);
	printf("best  matching is %30s (min     tcc %5d)\n",(mintcc_match[i]?mintcc_match[i]:"not serialized"),(int)mintcc_val[i]);
	printf("worst matching is %30s (max     tcc %5d)\n",(maxtcc_match[i]?maxtcc_match[i]:"not serialized"),(int)maxtcc_val[i]);
      }
      
      while((nb = getIds(methods,where, nb_w, data,mincew_id[data->m],maxcew_id[data->m])) != 0){
	plot_x = U_malloc(nb,double*,"refinestats error code 5\n");
	plot_y = U_malloc(nb,double*,"refinestats error code 6\n");
	fields = U_malloc(nb,char*,"refinestats error code 7\n");
	fields_id = U_malloc(nb,int,"refinestats error code 8\n");
	for(i=0;i<nb;i++){
	  fields_id[i] = i;
	  fields[i] = U_malloc(20,char,"refinestats error code 9\n");
	  sprintf(fields[i],"result %d",where[methods[i]]);
	  plot_y[i] = U_malloc(rstats[methods[i]]->levels+2,double,"refinestats error code 10\n");
	  plot_y[i][0] = rstats[methods[i]]->levels+1;
	  for(j=0;j<=rstats[methods[i]]->levels;j++){
	    plot_y[i][j+1] = j;
	  }
	  plot_x[i] = MD_getRefineData(rstats[methods[i]],x,x_stat,x_value,x_type);
	}
	draw = PLOT_xy(plot_y,plot_x, fields, fields_id, nb, "level",label[x],"refinement evolution",NO_BORDER,GRID,KEY,XTICS,YTICS);
	PLOT_draw(draw,&ctrl,NULL);
	printf("press enter to continue\n");
	getchar();
	PLOT_close(draw,&ctrl);
	PLOT_free(draw,&ctrl);
	for(i=0;i<nb;i++)
	  U_free("refinestats error code 11\n",&(plot_y[i]),&(plot_x[i]),
		  &(fields[i]),LEND);
	U_free("refinestats error code 12\n",&(plot_x),&(plot_y),
		&(fields),&(fields_id),LEND);
      }
      
      break;
    }
    printf("-------------------------------------------------------------------------------------------------------\n");
    printf("| %99s |\n","Refinement level information");
    printf("-------------------------------------------------------------------------------------------------------\n");
    printf("| %3s | %20s | %70s |\n","","option","value");
    printf("-------------------------------------------------------------------------------------------------------\n");
    printf("| %3d | %20s | %70s |\n",1,"plot output",plot_file);
    printf("| %3d | %20s | %70s |\n",2,"value",label[x]);
    printf("-------------------------------------------------------------------------------------------------------\n");
    printf("| %3d | %93s |\n",3,"back");
    printf("| %3d | %93s |\n",4,"plot");
    printf("-------------------------------------------------------------------------------------------------------\n");
    printf("choice : ");
  }while(scan_choice(&choice) && choice != 3);
  printf("\n");

  if(data != NULL){
    LD_free(data);
    MD_freeArrR(rstats,nb_w);
  }
  U_free("refinestats error code 4\n",&(where),&(match),LEND);
  
  U_free("logfile error code 1\n",&mincew_match,&mincew_val,&maxcew_match,&maxcew_val,
	 &mintcc_match,&mintcc_val,&maxtcc_match,&maxtcc_val,&mincew_id,&maxcew_id,
	 &mintcc_id,&maxtcc_id,LEND);
  
  return 1;
}

int refinedxoutput(logFinfo* info){
  int nb,i,j,k,methods[MAX_M];
  Graph **graphs;
  logF *data = NULL;
  RefineStats **rstats = NULL;
  char filename[MAX_S];
  int *where;
  int *match;
  int max_w = 100, nb_w = 0;
  ctrl ctrl;
  FILE* fp;

  int *mincew_id,*maxcew_id,*mintcc_id,*maxtcc_id;
  char **mincew_match,**maxcew_match,**mintcc_match,**maxtcc_match;
  double *mincew_val,*maxcew_val,*mintcc_val,*maxtcc_val;

  /* initialization */
  where = U_malloc(max_w,int,"refinestats error code 1\n");
  match = U_malloc(max_w,int,"refinestats error code 2\n");
  strcpy(filename,info->base);
  strcat(filename,".bet.log");

  /* default options */
  ctrl.multiple_win = NO;
  ctrl.plot_out = STDOUT;
  ctrl.stat_out = STDOUT;
  ctrl.plot_file = NULL;
  ctrl.stat_file = NULL;

  WARNING(((fp = fopen(filename,"r")) == NULL,"refinestats error code 3 : cannot open %s\n",filename),0);
  data = LD_readBetLog(fp,&ctrl);
  fclose(fp);
  /* from data get matchings and where ids */
  for(i=0;i<data->m;i++){
    for(j=0;j<data->s[i];j++){
      /* rampage not a too big array (<1000) search the whole array 
	 each time */
      for(k=0;k<nb_w;k++){
	if(where[k] == data->id[i][j])
	  break;
      }
      if(k == nb_w){
	where[k] = data->id[i][j];
	match[k] = data->matching[i][j];
	nb_w++;
	if(nb_w >= max_w){
	  max_w *= 2;
	  where = U_realloc(where,max_w,int,"refinestats error code 4\n");
	  match = U_realloc(match,max_w,int,"refinestats error code 4\n");
	}
      }
    }
  }
  /* done with searching those ids */
  rstats = MD_readRefineStats(data->coarse,match,where,info->base,nb_w,NULL);
  LD_logBestByMethod(data,LOG_CUTEDGE,LD_MIN,&mincew_match,&mincew_val,&mincew_id);
  LD_logBestByMethod(data,LOG_CUTEDGE,LD_MAX,&maxcew_match,&maxcew_val,&maxcew_id);
  LD_logBestByMethod(data,LOG_TOTCOMMCOST,LD_MIN,&mintcc_match,&mintcc_val,&mintcc_id);
  LD_logBestByMethod(data,LOG_TOTCOMMCOST,LD_MAX,&maxtcc_match,&maxtcc_val,&maxtcc_id);

	
  for(i=0;i<=data->m;i++){
    if(i<data->m)
      printf("%s :\n",data->methods[i]);
    else
      printf("overall :\n");
    printf("-----------------------------------------------\n");
    printf("best  matching is %30s (min cutedge %5d)\n",mincew_match[i],(int)mincew_val[i]);
    printf("worst matching is %30s (max cutedge %5d)\n",maxcew_match[i],(int)maxcew_val[i]);
    printf("best  matching is %30s (min     tcc %5d)\n",mintcc_match[i],(int)mintcc_val[i]);
    printf("worst matching is %30s (max     tcc %5d)\n",maxtcc_match[i],(int)maxtcc_val[i]);
  }
  
  while((nb = getIds(methods,where, nb_w, data,mincew_id[data->m],maxcew_id[data->m])) != 0){
    /* get graphs */
    graphs = MD_getGraphsRefine(data->coarse,match,where,methods,info->base,nb);
    
    for(i=0;i<nb;i++){
      DX_refinement(graphs[i],info->base,info->coords,where[methods[i]],
		    rstats[methods[i]]->cutedge,rstats[methods[i]]->imbalance,
		    rstats[methods[i]]->met[0]);
    }
    for(i=0;i<nb;i++){
      MD_freeGraph(graphs[i]);
    }
    U_free("refinedxoutput error code 6\n",&graphs,LEND);
  }
  U_free("refinedxoutput error code 7\n",&where,&match,LEND);
  LD_free(data);
  MD_freeArrR(rstats,nb_w);

  U_free("logfile error code 1\n",&mincew_match,&mincew_val,&maxcew_match,&maxcew_val,
	 &mintcc_match,&mintcc_val,&maxtcc_match,&maxtcc_val,&mincew_id,&maxcew_id,
	 &mintcc_id,&maxtcc_id,LEND);

  return 1;
}
int geomviewapp(logFinfo* info){ 
  GV_plotgraph(info->F_graph,info->coords);
  
  return 1;
}
int geomviewpart(logFinfo* info){
  char filename[MAX_S],stat_file[MAX_S];
  FILE* fp;
  ctrl ctrl;
  int nb=0, methods[MAX_M];
  logF *data = NULL;
  int max_w = 100, nb_w = 0;
  int *where;
  int *match;
  int i,j,k;
  char F_where[MAX_S];

  /* get results to plot */
  strcpy(stat_file,info->base);
  strcat(stat_file,".stat.log");

  ctrl.stat_out     = FILES;
  ctrl.stat_file    = stat_file;

  where = U_malloc(max_w,int,"refinestats error code 1\n");
  match = U_malloc(max_w,int,"refinestats error code 2\n");
  
  strcpy(filename,info->base);
  strcat(filename,".log");
  

  WARNING(((fp = fopen(filename,"r")) == NULL,
	   "logfile error code 1 : Cannot open file %s\n",filename),1);

  data = LD_readLog(fp,&ctrl);

  fclose(fp);

  /* from data get matchings and where ids */
  for(i=0;i<data->m;i++){
    for(j=0;j<data->s[i];j++){
      /* rampage not a too big array (<1000) search the whole array 
	 each time */
      for(k=0;k<nb_w;k++){
	if(where[k] == data->id[i][j])
	  break;
      }
      if(k == nb_w){
	where[k] = data->id[i][j];
	match[k] = data->matching[i][j];
	nb_w++;
	if(nb_w >= max_w){
	  max_w *= 2;
	  where = U_realloc(where,max_w,int,"refinestats error code 4\n");
	  match = U_realloc(match,max_w,int,"refinestats error code 4\n");
	}
      }
    }
  }  

  while((nb = getIds(methods,where, nb_w, data,-1,-1)) != 0){
    for(i=0;i<nb;i++){
      sprintf(F_where,"%s.result.%d",info->base,where[i]);
      printf("reading result %s\n",F_where);
      GV_plotpart(info->F_graph,info->coords,F_where);
    }
  }
  
  return 1;
}
